export function setupCounter(element) {
  let counter = 0;

  const setCounter = (count) => {
    counter = count;
    element.innerHTML = `count is ${counter}`;
  };

  element.addEventListener('click', () => {
    const baseUrl = window.location.origin;
    const requestUrl = `${baseUrl}/click`;

    fetch(requestUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ count: counter  })
    })
        .then(response => {
          console.log('Response status:', response.status);
          return response.json();
        })
        .then(data => {
          if (data.newCount !== undefined) {
            setCounter(data.newCount);
          } else {
            console.error('Invalid response format:', data);
          }
        })
        .catch(error => console.error('Error:', error));
  });

  setCounter(0);
}

// export function setupCounter(element) {
//   let counter = 0
//   const setCounter = (count) => {
//     counter = count
//     element.innerHTML = `count is ${counter}`
//   }
//   element.addEventListener('click', () => setCounter(counter + 1))
//   setCounter(0)
// }
